package com.atguigu.crud.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;


@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer, BeanFactoryAware {

    private BeanFactory beanFactory;

    @Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        DataSource dataSource = beanFactory.getBean(DataSource.class);
        return new DataSourceTransactionManager(dataSource);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}
